package com.HRs.UI;

import com.HRs.Controller.PatientController;
import com.HRs.Controller.aDController;
import com.HRs.Controller.dtAController;
import com.HRs.utils.Account;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login {
    JFrame frame = new JFrame("医院预约系统");
    JLabel TitleLbl = new JLabel();
    JPanel UserPanel;
    JPanel PasswordPanel;
    JPanel IdentityPanel;
    JPanel ButtonPanel;
    JPanel ImagePanel;

    public Login(){
        frame.setLayout(new GridLayout(7, 1));
        //--------------背景图片设置---------------
//        ImageIcon background = new ImageIcon("JDBC\\images\\UM1QRI)4W0F6Y3N$QYH~G_C.png");//背景图片
        ImageIcon background = new ImageIcon("JDBC\\images\\bg.jpeg");//背景图片
        //设置背景图片的大小与窗体一致
        background.setImage(background.getImage().getScaledInstance(1000, 800, Image.SCALE_DEFAULT));

        JLabel label1 = new JLabel(background);//把背景图片显示在一个标签里

        label1.setSize(1000, 800);
        ImagePanel = (JPanel) frame.getContentPane();
        ImagePanel.setOpaque(false);
//        ImagePanel.setLayout(new GridLayout(6, 1));
        frame.getLayeredPane().setLayout(null);
        frame.getLayeredPane().add(label1, Integer.valueOf((Integer.MIN_VALUE)));

        //----------------文字标题标签------------------
        TitleLbl = new JLabel();
        TitleLbl.setText("医 院 预 约 系 统");
        TitleLbl.setFont(new Font("隶书", Font.BOLD, 75));
        TitleLbl.setHorizontalAlignment(SwingConstants.CENTER);
        frame.add(BorderLayout.NORTH, TitleLbl);

        //----------------主体----------------------
//        JLabel lblLogin = new JLabel("登录", JLabel.CENTER);
//        lblLogin.setFont(new Font("隶书", Font.PLAIN, 30));
//        frame.add(lblLogin);

        JPanel p = new JPanel();
        JLabel lbl = new JLabel("");
        p.add(lbl);
        frame.add(p);

        JLabel lbluser = new JLabel("   用户名  ");
        lbluser.setFont(new Font("隶书", Font.PLAIN, 30));
        JTextField txtUser = new JTextField(20);
        txtUser.setFont(new Font("隶书", Font.PLAIN, 25));
//        txtUser.setFont(new Font("隶书", Font.PLAIN, 15));
        UserPanel = new JPanel();
        UserPanel.add(lbluser);
        UserPanel.add(txtUser);
        frame.add(UserPanel);

        JLabel lblpassword = new JLabel("   密  码  ");
        JPasswordField txtpwd = new JPasswordField(20);
        txtpwd.setFont(new Font("隶书", Font.PLAIN, 25));
        lblpassword.setFont(new Font("隶书", Font.PLAIN, 30));
        PasswordPanel = new JPanel();
        PasswordPanel.add(lblpassword);
        PasswordPanel.add(txtpwd);
        frame.add(PasswordPanel);

        JLabel lblid = new JLabel("   身  份  ");
        lblid.setFont(new Font("隶书", Font.PLAIN, 25));
        JComboBox<String> box = new JComboBox<>();
        box.setFont(new Font("隶书", Font.PLAIN, 20));
        JTextField f = new JTextField(20);
        box.addItem("---请选择身份---");
        box.addItem("病人");
        box.addItem("管理员");
        box.addItem("科室管理员");
        IdentityPanel = new JPanel();
        IdentityPanel.add(lblid);
        IdentityPanel.add(box);
        frame.add(IdentityPanel);

        JButton b1 = new JButton("登  录");
        b1.setFont(new Font("隶书", Font.ITALIC, 30));
//        b1.setContentAreaFilled(false);
        b1.addActionListener(new ActionListener() {//登录
            @Override
            public void actionPerformed(ActionEvent e) {
                String id = txtUser.getText();
                String password = txtpwd.getText();
                String type = (String) box.getSelectedItem();
                PatientController PC = new PatientController();
                dtAController dC = new dtAController();
                aDController aC = new aDController();
                if(type.equals("病人")){
                    if(PC.Login_p(id, password)){
                        Account.p_id = id;
                        Account.p_password = password;
                        new Hall_Patient();
                        frame.dispose();
                    }else{
                        JOptionPane.showMessageDialog(b1, "用户名或密码错误！", "提示消息", JOptionPane.ERROR_MESSAGE);
                    }
                }else if (type.equals("科室管理员")) {
                    if(dC.Login_dtA(id, password)){
                        Account.dta_id = id;
                        Account.dta_password = password;
                        new Hall_dtA();
                        frame.dispose();
                    }else{
                        JOptionPane.showMessageDialog(b1, "用户名或密码错误！", "提示消息", JOptionPane.ERROR_MESSAGE);
                    }
                }else if (type.equals("管理员")){
                    if(aC.Login_aD(id, password)){
                        Account.a_id = id;
                        Account.a_password = password;
                        new Hall_Admin();
                        frame.dispose();
                    }else{
                        JOptionPane.showMessageDialog(b1, "用户名或密码错误！", "提示消息", JOptionPane.ERROR_MESSAGE);
                    }
                }

            }
        });

        JButton b2 = new JButton("注  册");
        b2.setFont(new Font("隶书", Font.ITALIC, 30));
//        b2.setContentAreaFilled(false);
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Register();
                frame.dispose();
            }
        });

        ButtonPanel = new JPanel();
        ButtonPanel.add(b1);
        ButtonPanel.add(b2);
        frame.add(ButtonPanel);

        p.setOpaque(false);
        UserPanel.setOpaque(false);
        PasswordPanel.setOpaque(false);
        ButtonPanel.setOpaque(false);
        IdentityPanel.setOpaque(false);

        frame.setSize(1000, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Login();
    }
}
